package com.java.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.java.dao.IndexSlideMapper;
import com.java.model.*;
import com.java.service.IndexSlideService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

import static java.lang.Integer.parseInt;


@Service
public class IndexSlideServiceImpl implements IndexSlideService {

    @Autowired
    private IndexSlideMapper indexSlideMapper;

    @Override
    public Result save(IndexSlide indexSlide) {
        indexSlideMapper.save(indexSlide);
        return Result.success();
    }

    @Override
    public Result modify(IndexSlide indexSlide) {
        indexSlideMapper.modify(indexSlide);
        return Result.success();
    }

    @Override
    public Result remove(int[] ids) {
        for (int id : ids) {
            indexSlideMapper.remove(id);
        }
        return Result.success();
    }

    @Override
    public Result list(IndexSlide indexSlide) {
        PageHelper.startPage(indexSlide.getPageNum(), indexSlide.getPageSize()); // 提醒page在显示哪一页的数据和要显示多少条数据
        List<IndexSlide> list = indexSlideMapper.list(indexSlide);
        PageInfo<IndexSlide> pageInfo = new PageInfo<>(list);

        return Result.success(pageInfo.getList(), pageInfo.getTotal());
    }

    @Override
    public Result get(int id) {
        IndexSlide indexSlide = indexSlideMapper.get(id);
        return Result.success(indexSlide, null);
    }

    @Override
    public Result indexSlideStatus(int[] ids, String status) {
        for (Integer id : ids) {
            IndexSlide indexSlide = new IndexSlide();
            indexSlide.setId(id);
            indexSlide.setStatus(parseInt(status));
            indexSlideMapper.indexSlideStatus(indexSlide);
        }
        return Result.success();
    }

    @Override
    public Result statusList(IndexSlide indexSlide) {
        List<IndexSlide> indexSlides = indexSlideMapper.statusList(indexSlide);
        return Result.success(indexSlides, null);
    }
}
